<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <!--{include file='comm/header.tpl'}-->
    <title>商品单位</title>
</head>

<body>

<!--{include file='comm/naviBanner.tpl'}-->

<div class="ebsig_container">

    <!--{include file='comm/menu.tpl'}-->
    <div class="comm_content">

            <div class="search-box">
                <div style="position:relative">
                    <form id="search_form" onsubmit="return false;">
                        <table>
                            <tbody>
                                <tr>
                                    <td height="27px">
                                        <span>单位名称：<input type="text" id="s_unitName" name="unitName" value="" /></span>
                                        <span><input type="button" value="查询" class="btn" onclick="unit.search();" /></span>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </form>
                </div>
            </div>

            <table id="flex" style="display:none"></table>

        </div>

</div>

<div id="pop" style="display: none;">
    <div style="width:100%; background: #ffffff">

        <form id = "unit_form" onsubmit="return false;">

            <div class="input-group input-group-lg"  >
                <span class="input-group-addon">单位名称：</span>
                <input class="form-control"  type="text" id="unitName" name="unitName" maxlength="20" placeholder="请输入单位名称 【必填项】"  value="" />
            </div>

            <div class="input-group input-group-lg"  >
                <span class="input-group-addon">排列序号：</span>
                <input class="form-control"  type="text" id="sortOrder" name="sortOrder" maxlength="20" placeholder="请输入排列序号 【必填项】"  />
            </div>
            <input type="hidden" name="useFlg" id="useFlg" value= "1" />
            <input type="hidden" name="unitID" id="unitID" value= "" />
        </form>
    </div>
</div>

<script type="text/javascript">
$("#flex").flexigrid({
    url:"/invokingAjax-backend/goods/unit.ajax?operFlg=1&xjx=none",
    dataType: "json",
    colModel : [
        <!--{if $edit_permission}-->
        {display: "操作", width : 100, align: "center"},
        <!--{/if}-->
        {display: "单位号", width : 100},
        {display: "单位名称", width : 280},
        {display: "排列序号", width : 100,sortable : true,name:"sortOrder"},
        {display: "使用状态", width : 100 , align: "center"}
	],
    <!--{if $edit_permission}-->
    buttons : [
        {name: "添加", bclass: "add", onpress : "unit.edit"},
        {separator: true},
        {name: "删除", bclass: "delete", onpress : "unit.deleteUnit"}
    ],
    <!--{/if}-->
    sortname: "sortOrder",
    sortorder: "desc",
    usepager: true,
    rp: 10,
    useRp: true,
    showTableToggleBtn: true,
    onSubmit: function() {
        var dt = $('#search_form').serializeArray();
        $("#flex").flexOptions({params: dt});
        return true;
    }
});

    var sortNum = "";

    var unit = {

        delete_str: "" ,
		useFlg_str : "" ,
		
        //搜索单位
        search: function() {
            var dt = $('#search_form').serializeArray();
            $("#flex").flexOptions({params: dt}).flexReload();
        },


        deleteUnit: function() {
            this.delete_str = "";
            $.each($("#flex").find("tr"), function(k, v) {
                if ($(v).hasClass("trSelected")) {
                    unit.delete_str += "_" + $(v).attr("id").replace("row", "");
                }
            });

            if (this.delete_str == "")
                E.alert("请选择需要删除的商品单位");
            else
                E.confirm("您确认要删除选中的商品单位吗？", "unit.deleteConfirm");
        },
        
		doAction: function( unitID , useFlg ) {
            this.useFlg_str = unitID + "_" + useFlg ;
            if( useFlg == 0 )
            	E.confirm("您确认要禁用此商品单位吗？", "unit.actionConfirm");
            else
            	unit.actionConfirm() ;
        },
        
        actionConfirm : function () {
        	E.loadding.open('请稍候...');
            $.ajax({
                type: "POST",
                url: "/invokingAjax-backend/goods/unit.ajax?operFlg=5&useFlg_str=" + unit.useFlg_str,
                dataType: "JSON",
                success: function( result_obj ) {
                	 E.loadding.close();
                    if (result_obj.code == 200) {
                        E.alert(result_obj.message, 2, "unit.load");
                    } else {
                        E.alert(result_obj.message);
                    }
                }
            });
        },
        
        deleteConfirm: function() {
            this.delete_str = this.delete_str.substr(1);
            $.ajax({
                type: "POST",
                url: "/invokingAjax-backend/goods/unit.ajax?operFlg=4&delete_str=" + unit.delete_str,
                dataType: "JSON",
                success: function( result_obj ) {
                    if (result_obj.code == 200) {
                        E.alert(result_obj.message, 2, "unit.load");
                    } else {
                        E.alert(result_obj.message);
                    }
                }
            });
        },

        //编辑或添加
        edit: function(unitID){
            E.loadding.open('请稍候...');
            var name="";
            if(unitID){

                name = "修改商品单位";
                E.ajax_post({
                    url: "/invokingAjax-backend/goods/unit.ajax?operFlg=6&xjx=none",
                    data:{unitID:unitID},
                    call: "unit.unitEdit"
                });
            }else{
            	name = "添加商品单位";
                E.ajax_get({
                    url:"/invokingAjax-backend/goods/unit.ajax?operFlg=7",
                    data:"" ,
                    call:"unit.sortUp"
                });
            }

            E.popup.open({
                id:"pop",
                title: name,
                content: "",
                css: "width:960px;",
                btnName: "保存",
                btnCall:"unit.check();"
            });

        },

        unitEdit: function( o ) {
        	E.loadding.close();
            $("#unitName").val(o.unitName);
            $("#sortOrder").val(o.sortOrder);
            $("#useFlg").val(o.useFlg);
            $("#unitID").val(o.unitID);
        },

        load: function() {
            $("#flex").flexReload();
        },


        check: function() {

            var dt = E.getFormValues("unit_form");
            var error_msg = "";
            if (dt.unitName == "")
                error_msg += "单位名称不能为空<br />";

            if (!E.isInt(dt.sortOrder))
                error_msg += "排序序号必须为数字<br />";

            if (error_msg != "")
                E.alert(error_msg);
            else
                unit.addSave() ;
        },

        //保存单位信息
        addSave: function() {
            var dt = E.getFormValues("unit_form");
            var id = dt.unitID;
            $.ajax({
                type: "POST",
                url: "/invokingAjax-backend/goods/unit.ajax?operFlg=2" ,
                dataType: "JSON",
                data: dt,
                success: function( result_obj ) {
                    if (result_obj.code == 200) {
                        unit.load();
                        if(id){
                            E.alert(result_obj.message, 2, "E.popup.close");
                        }else{
                            E.alert(result_obj.message, 2);
                            sortNum =  result_obj.sort;
                            $("#unitName").val('');
                            $("#sortOrder").val( result_obj.sort );
                        }
                    } else {
                        E.alert(result_obj.message);
                    }
                }
            });
        },
        sortUp: function(o){
        	E.loadding.close();
            $("#sortOrder").val(o.sortOrder);
        }

    };
</script>

</body>

</html>
